package edu.nus.cs4243.recon.ui;

import java.util.HashSet;
import java.util.Set;

import edu.nus.cs4243.recon.logic.Camera;
import edu.nus.cs4243.recon.model.Feature;
import edu.nus.cs4243.recon.model.project.Project;

/**
 * Holds state shared by all ImageControls (selected Feature and selected Camera)
 * 
 * @author johannes
 */
public class ImageControlGlobalState {

	private static final ImageControlGlobalState instance = new ImageControlGlobalState();

	private Feature selectedFeature;
	private Set<ImageControl> imageControls = new HashSet<ImageControl>();
	private Camera camera;
	private Project project;

	private ImageControlGlobalState() {
	}

	public static ImageControlGlobalState get() {
		return instance;
	}

	public static ImageControlGlobalState getState() {
		return get();
	}

	/**
	 * Return the currently selected feature.
	 * 
	 * @return
	 */
	public Feature getSelectedFeature() {
		return selectedFeature;
	}

	/**
	 * Set the currently selected feature.
	 * 
	 * @param f
	 */
	public void setSelectedFeature(Feature f) {
		if (selectedFeature != f) {
			selectedFeature = f;
			notifyFeatureChange(f);
		}
	}

	/**
	 * Register a new ImageControl
	 * 
	 * @param i the new ImageControl
	 */
	protected void addImageControl(ImageControl i) {
		imageControls.add(i);
	}

	/**
	 * Notifies all registered ImageControls that a given feature has changed.
	 * 
	 * @param f
	 *            the feature that has changed.
	 */
	public void notifyFeatureChange(Feature f) {
		for (ImageControl i : imageControls) {
			i.featureChanged(f);
		}
	}

	/**
	 * Set the currently selected camera.
	 * 
	 * @param camera
	 */
	public void setCamera(Camera camera) {
		this.camera = camera;
	}

	/**
	 * Return the currently selected camera.
	 * 
	 * @return
	 */
	public Camera getCamera() {
		return camera;
	}
	
	/**
	 * Get the current project.
	 * 
	 * @return
	 */
	public Project getProject() {
		return project;
	}
	
	/**
	 * Set the current project.
	 * 
	 * @param project
	 */
	public void setProject(Project project) {
		this.project = project;
	}

}
